/*******************************************************************************
* Purpose: Preprocess 12-month HOME instrument
* 
* Dependencies:
*   Input: DSC0043.dta, 12-Months HOME assessment
*   Output: HOME_12m.dta
*
* Notes: -
*******************************************************************************/
set more off
clear all

global storage: env storage

use "$storage/ece_parenting/raw_data/DSC0001.dta"
keep IHDP PAG
sort IHDP
merge 1:1 IHDP using "$storage/ece_parenting/raw_data/DSC0043.dta", nogenerate

// keep primary sample
keep if PAG == 1

// drop all observation where at least one response is missing
foreach var of varlist VA1-VA45 {
  drop if missing(`var') 
}

// order variables 
order IHDP VA*, sequential

// construct subscales
gen alt_subscale_1_12 		= (VA1 + VA2 + VA3 + VA8 + VA9 + VA10 + VA11) / 7 
gen alt_subscale_2_12 		= (VA4 + VA5 + VA6) / 3
gen alt_subscale_3_12 		= (VA12 + VA13 + VA14 + VA16 + VA17) / 5
gen alt_subscale_4_12 		= (VA18 + VA26 + VA27 + VA29 + VA30 + VA31 + ///
							   VA33 + VA34 + VA42 + VA45) / 10
gen alt_subscale_5_12 		= (VA21 + VA22 + VA23) / 3
gen alt_subscale_6_12 		= (VA37 + VA38 + VA39 + VA40) / 4

label variable alt_subscale_1_12 "Parental Warmth"
label variable alt_subscale_2_12 "Parental Verbal Skills"
label variable alt_subscale_3_12 "Parental Lack of hostility"
label variable alt_subscale_4_12 "Learning/Literacy"
label variable alt_subscale_5_12 "Activities/Outgoings"
label variable alt_subscale_6_12 "Developmental Advance"

// total score
egen alt_parenting_12_raw = rowtotal(VA1 VA2 VA3 VA4 VA5 VA6 VA8 VA9 VA10    /// 
									 VA11 VA12 VA13 VA14 VA16 VA17 VA18 VA26 ///
									 VA27 VA29 VA30 VA31 VA33 VA34 VA42 VA45 ///
									 VA21 VA22 VA23 VA37 VA38 VA39 VA40)
// drop indicator variables
drop VA*

// construct factor
factor alt_subscale_1_12 alt_subscale_2_12 alt_subscale_3_12 ///
	   alt_subscale_4_12 alt_subscale_5_12 alt_subscale_6_12
predict parenting_12_alt

// save 
capture mkdir "$storage/ece_parenting/intermediate_data/HOME"
save "$storage/ece_parenting/intermediate_data/HOME/HOME_12m.dta", replace
